﻿using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace SalesManagementCommon.API.SalesManagementCommons.SalesOrderSearch
{
    public class SalesOrderQueryHander : IRequestHandler<SalesOrderQueryCommand, ResultPage<SalesOrderDTO>>
    {
        private readonly IBaseRepository<SalesOrder> _salesOrderRequest;

        public SalesOrderQueryHander(IBaseRepository<SalesOrder> salesOrderRequest)
        {
            _salesOrderRequest = salesOrderRequest;
        }

        /// <summary>
        /// 处理销售订单单据查询请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SalesOrderDTO>> Handle(SalesOrderQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SalesOrderDTO>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PageCount = 0 };
            try
            {
                var salesOrders = await (await _salesOrderRequest.GetAll()).ToListAsync();
                var ss = (from s in salesOrders
                          select new SalesOrderDTO()
                          {
                              DocumentNumber = s.DocumentNumber,
                              DocumentStatus = s.DocumentStatus,
                              CustomerProfileName = s.CustomerProfileName,
                              SalesStaff = s.SalesStaff,
                              BusinessType = s.BusinessType,
                              BusinessStatus = s.BusinessStatus,
                              AuditDate = s.AuditDate,
                              DocumentDate = s.DocumentDate,
                          }).ToList();
                if (!string.IsNullOrEmpty(request.DocumentNumber)) //单据编码
                {
                    ss = ss.Where(p => p.DocumentNumber == request.DocumentNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.CustomerProfileName))//客户名称
                {
                    ss = ss.Where(p => p.CustomerProfileName == request.CustomerProfileName).ToList();
                }
                if (!string.IsNullOrEmpty(request.BusinessStatus))//业务状态
                {
                    ss = ss.Where(p => p.BusinessStatus == request.BusinessStatus).ToList();
                }
                if (!string.IsNullOrEmpty(request.SalesStaff))//销售人员
                {
                    ss = ss.Where(p => p.SalesStaff.Contains(request.SalesStaff)).ToList();
                }
                if (!string.IsNullOrEmpty(request.DocumentStatus))//单据状态
                {
                    ss = ss.Where(p => p.DocumentStatus == request.DocumentStatus).ToList();
                }

                result.TotalCount = ss.Count;
                result.PageCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                ss = ss.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = ss;
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;

        }
    }
}
